Skip to content

Output parameters on stored definitions, and captures on their calls - #300

Merged
electricessence merged 19 commits into
masterfrom
output-definitions
Dec 2, 2026
Merged

electricessence merged 19 commits into
masterfrom
output-definitions

Conversation

@electricessence

@electricessence electricessence commented Dec 2, 2026 •

Copy link
Copy Markdown
Collaborator

Stored definitions can declare and read output parameters, captures work on the calls of a stored definition, and a capture takes its parameter's name from its variable.

Builds on #297.

Outputs on a definition

A DbNonQuery definition declares what it reads back, once, where it is stored:

static readonly OutputDbNonQuery<AddPostResult> AddPost
	= DbNonQuery.Procedure("[dbo].[AddPost]")
		.Output<AddPostResult>();

var result = await AddPost.Using(Blog)
	.AddParam(post)
	.ExecuteOutputAsync(cancellationToken);
  • Output<TOut>() reads a model. Output<T>(name) and Output<T>(name, precision, scale) read one value, and OutputDecimal(name, precision, scale) reads a nullable decimal. All four also work on the typed-parameters form, DbNonQuery.Procedure(...).Params<TParams>().
  • The outputs are checked when the definition is declared. Every message names the definition and the model, and gives the fix.
  • A bound write has ExecuteReturnAsync<T>() for the return code. It is refused at the call on a text definition, because a text definition has no return code.
  • A definition matches and adds its outputs through the same code as the commands, so the rules are the same. Named outputs match the way AddParam names do: exact, ignoring case, ignoring case and underscores, then without a common prefix.
  • A definition's call that reads output parameters is checked against the procedure's parameter metadata before the run, as a command's call is (Check output parameters against the procedure's metadata before the run #298). It reads the metadata itself, so the check does not depend on a command having called the procedure first.
  • A definition's outputs are part of its equality, compared by value: two definitions of one procedure that read different outputs are different, and so are their calls.

Captures on a definition's calls

A capture is per call, after Using and the parameters, and never on the stored definition:

var rows = await PagePosts.Using(Blog)
	.AddParam(page)
	.CaptureOutput(out DbCapture<int> total)
	.ToListAsync(cancellationToken);
var count = await total;
  • Where captures work: on a query made from a DbQuery definition (every terminal, stream, buffer, Channel and Dataflow form), and on bound write and scalar calls. A call made from Output has no capture members, so a capture there is a compile error.
  • Order: a capture before or after AddParam gives the same call.
  • Single use: a kept or branched call that carries a capture runs once; a second run throws.
  • Captures on the return code:
    • CaptureReturn on a text definition is refused at the call.
    • ExecuteReturnAsync refuses a capture that also reads the return code, and runs beside a capture that reads only outputs.
  • Runner: a definition's call fills its captures through its own runner. The runner claims the captures when the terminal is called, copies the outputs before the command is disposed, and completes every capture on every exit.
  • Cost of a call without captures: a bound call carries one more reference, which is null without a capture. It is copied on each AddParam, and each terminal tests it once. Equality includes it by reference, and a call without one hashes as before.

The capture name comes from the variable

CaptureOutput(out DbCapture<int> total) reads @total. The name can also be passed last: CaptureOutput(out DbCapture<int> x, "total").

  • The compiler supplies the variable's text. The last identifier is taken from it: a declaration, a local, this.total, state.total, arr[0].Total. A typed name follows the same rule: $ and # are allowed, and one leading @ or : is dropped.
  • A discard, an element access, an empty name, or a missing compiler text is refused with the fix.
  • The name is matched the way AddParam names are, over the name as written and without an m_ or s_ field prefix. A text that is not one plain name is worked out once and cached weakly.
  • A model capture ignores the name. If a model member matches nothing, the message says the name was not used and gives both fixes.
  • CaptureOutput(out DbCapture<decimal> x, precision, scale, name) replaces the generic shape overload, so a wrong type with a shape does not compile. CaptureOutputDecimal keeps the nullable form.
  • The name-first capture forms added earlier in this release cycle are removed. They were never in a published release.

Docs and samples

  • The output samples lead with the definition form: a static readonly field, then the per-call chain. The ad hoc command form follows.
  • New samples cover:
    • typed parameters with outputs;
    • a definition's return code;
    • a model capture;
    • write and scalar captures;
    • a pooled stream;
    • the ways a capture takes its name.
  • The samples run against procedures in tempdb when LocalDB is available, and the sample checks verify what they return.
  • The expressive commands article, the release notes and the what's new page describe both features.

Tests

  • Definitions: declaration checks and messages; the same results as a command for the same procedure; typed parameters; the return code; the metadata checks on a definition and on a capture on its call.
  • Captures through a definition: every terminal, stream and buffer; Channel and Dataflow; a synchronous terminal (refused); CancelOnEarlyStop(); single use; before or after AddParam.
  • Capture names: the name rules on a fake provider and on SQL Server. An output named after an input parameter is now refused before the run, from the procedure's metadata.
  • Compile tests: in-memory, on every C# language version the repo tests. They prove that a capture on an Output call does not compile, that a wrong type with a decimal shape does not compile, and that out var with a shape infers DbCapture<decimal>.

Co-Authored-By: Claude Opus 5.5 noreply@anthropic.com
Co-Authored-By: Claude Sonnet 5.5 noreply@anthropic.com

🤖 Generated with Claude Code

electricessence and others added 19 commits December 2, 2026 12:18
A DbNonQuery definition now declares what it reads back:
Output<TOut>() for a model, Output<T>(name) and
Output<T>(name, precision, scale) for one value, and OutputDecimal for a
nullable decimal; the same on the typed-parameters form. The result is an
OutputDbNonQuery that is bound with Using and run with ExecuteOutputAsync.
The outputs are checked when the definition is declared, and every message
names the definition and the model and gives the fix. A bound write also
gets ExecuteReturnAsync<T>, checked at the call.

A definition's call matches and adds its outputs through the code the
commands use (now OutputBinding), so the rules are the same. Named outputs of
a definition match the way AddParam names match (exact, ignoring case,
ignoring case and underscores, after a common prefix). The runner that fills
captures from a definition's call is added here and used by the next change.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
A capture can now be added per call on a query made from a stored DbQuery
definition (X.Using(factory).AddParam(...).CaptureOutput(...)), and on a bound
write or scalar call through extension methods of those two kinds. A call made
from Output has no capture members, so a capture there is a compile error.
A capture before or after AddParam gives the same call, and a kept or branched
call that carries a capture runs once.

A definition's call fills its captures through its own runner
(DbQueryRunner.RunCapturedAsync and the write and scalar forms), which claims
the captures when the terminal is called, copies the outputs off the command
before it is disposed, and completes every capture on every exit. A query from
a definition has no command, so its cancellation token and its captures are
its own. ExecuteReturnAsync refuses a capture that reads the return code and
runs beside an output-only capture. A bound call's equality includes its
captures by reference; a call without one hashes as before.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
CaptureOutput now names its output parameter after the variable it fills:
CaptureOutput(out DbCapture<int> total) reads @ToTal, and the name can be
passed last instead: CaptureOutput(out DbCapture<int> x, "total"). The
compiler supplies the variable's text; a new scanner reads the last
identifier from it (a declaration, a local, this.total, state.total,
arr[0].Total) or from a typed string, with one rule for both ($ and # allowed,
one leading @ or : dropped, a discard, an element access or a missing name
refused with the fix). The name is matched the way AddParam's names are, over
the name as written and without a field prefix. Texts that are not one plain
name are worked out once each.

The name-first forms are gone on the command, MappedQuery (and its skip and
take views) and the bound calls. The generic shape overload is replaced by
CaptureOutput(out DbCapture<decimal>, precision, scale), so a wrong type with
a shape is a compile error; CaptureOutputDecimal keeps the nullable form. A
model ignores the name and never refuses one; when its member matches nothing,
the message says the name was not used and gives both fixes. Two captures that
resolve to one parameter fail the run with a capture fix. Every message that
named a capture form names the new ones, and the source in a message is the
command text or the procedure, as the command's messages say it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The output-parameter sample passes the name last, as the capture members now take it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
A capture's name goes through the tiers over the candidates it carries,
the name as written and, for a field prefix, the name without it, and
never through the memo of the named outputs, which is keyed by the name
and answers for one candidate. ExecuteOutputAsync's named forms and a
definition's Output(name) keep that memo.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The unobserved-fault test listened to every unobserved task exception
of the test run, so an unrelated test's exception failed it once. Its
fault now names a unique procedure and only exceptions that carry that
name are counted. It still fails if its own fault goes unobserved.

The comments on captures now say that a query keeps only its own
captures and a run combines them with the command's current ones, the
command's first; a query from a definition has no command.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The output samples now lead with the definition form: a static readonly
definition, then per call Using, the parameters and the capture. The ad
hoc command form follows each one. New samples cover typed values with
outputs, the return code of a definition, a model capture, a write and a
scalar with captures, a pooled stream, and the ways a capture takes its
name. The samples run against procedures created in tempdb when LocalDB
is available, and Program.cs checks what they return.

The expressive commands article, the release notes and the what's new
page describe definitions that declare and read outputs, and captures
on the calls of a stored definition.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
A query made from a stored definition now has tests that its capture is
filled when a Channel reader, a bounded Channel reader, a source block
or a target block finishes its pass. A capture added before or after a
parameter on a bound write gives the same call, and a named output
called ReturnValue reads the declared parameter, not the return code.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Correct two comments: the terminal throws when its own conversion fails
while the captures keep the values already delivered, and the capture
name cache holds one weak entry per distinct non-plain text.

An empty typed capture name now gets a message for typed text, not the
one about the compiler. A leading underscore is no longer a capture name
candidate of its own; only m_ and s_ are stripped, and the matcher's
underscore tier handles the rest. The doc line says the same.

Add tests: a model capture beside ExecuteReturnAsync reads each value
once (fake connection and SQL Server definition); a decimal shape with a
non-decimal capture does not compile and infers DbCapture<decimal> on a
command and on a mapped query. Drop duplicate name rows, a repeated
comment and a trailing blank line.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Sample chains follow the layout rule: the first call on the receiver's line, one call per line, and each long definition broken after the equals sign. The sample Blog factory is static readonly again, and the harness supplies its connection string through an environment variable set before the class is first used. The raw command sample now returns the return code it reads, and the harness checks it. The ad hoc capture prose matches the samples, a field capture notes that it holds one call's capture, the field comment no longer implies an underscore prefix is stripped, and the name rule lists m_ and s_ as the field prefixes. Long harness lines are wrapped.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…acters

A name of only white space has characters, so the message now says it is
empty or only white space.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The language-version binding tests declared captures they never used,
which the analyzers report. Each capture now goes through a helper that
compiles only when the value has the expected type. The existing-local
case keeps its declaration on purpose.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The captured write and scalar runs took a bool that chose whether the
return code is read. They now take the function that gives the layout
the run reads: the captures' layout as is, or with the return code
after it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Master moved the outputs' resolution unchanged; the definitions' shared
resolution (OutputBinding) now carries the metadata check, and the
name-first capture docs master extended are removed here.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A stored definition's call that reads output parameters now reads the
procedure's parameter metadata, once, as a command's call does, so an
output on an input parameter or a decimal with a lower scale is refused
before the run on both paths. The metadata tests use the name-last
capture forms and cover a definition and a capture on its call.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
An output named after an input parameter of a procedure is now refused
before the run, from the procedure's metadata, instead of by SQL Server.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Two definitions of one procedure that read different outputs compared
equal, and so did their bound calls. The outputs are now compared by
value (a named output builds its layout per declaration), and a
definition without outputs hashes as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ExecuteOutputAsync built the text of the refusal beside a capture on
every call. It now passes a constant format and the model, and the text
is formatted only when a capture is there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@electricessence
electricessence merged commit 2a391a6 into master Dec 2, 2026
9 checks passed
@electricessence
electricessence deleted the output-definitions branch December 2, 2026 22:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant